package com.youlai.boot.modules.game.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.modules.game.mapper.GameAchievementMapper;
import com.youlai.boot.modules.game.mapper.GameUserAchievementMapper;
import com.youlai.boot.modules.game.model.entity.GameAchievement;
import com.youlai.boot.modules.game.model.entity.GameUserAchievement;
import com.youlai.boot.modules.game.model.vo.GameAchievementVO;
import com.youlai.boot.modules.game.service.GameAchievementService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 游戏成就服务实现类
 *
 * @author 有来技术团队
 */
@Service
@RequiredArgsConstructor
public class GameAchievementServiceImpl extends ServiceImpl<GameAchievementMapper, GameAchievement> implements GameAchievementService {

    private final GameUserAchievementMapper gameUserAchievementMapper;

    @Override
    public List<GameAchievementVO> getUserAchievements(Long userId) {
        // 如果未提供用户ID，则使用当前登录用户ID
        if (userId == null) {
            userId = SecurityUtils.getUserId();
        }
        
        // 查询用户解锁的成就列表
        return baseMapper.getUserAchievements(userId);
    }

    @Override
    public List<GameAchievementVO> getGameAchievements(Long userId, Long gameId) {
        // 如果未提供用户ID，则使用当前登录用户ID
        if (userId == null) {
            userId = SecurityUtils.getUserId();
        }
        
        // 查询游戏的成就列表
        return baseMapper.getGameAchievements(userId, gameId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean unlockAchievement(Long userId, Long achievementId) {
        // 如果未提供用户ID，则使用当前登录用户ID
        if (userId == null) {
            userId = SecurityUtils.getUserId();
        }
        
        // 检查成就是否存在
        GameAchievement achievement = this.getById(achievementId);
        if (achievement == null) {
            return false;
        }
        
        // 检查用户是否已解锁该成就
        int count = gameUserAchievementMapper.checkUserAchievementUnlocked(userId, achievementId);
        if (count > 0) {
            // 已解锁，直接返回成功
            return true;
        }
        
        // 创建用户成就记录
        GameUserAchievement userAchievement = new GameUserAchievement();
        userAchievement.setUserId(userId);
        userAchievement.setAchievementId(achievementId);
        userAchievement.setUnlocked(true);
        userAchievement.setUnlockedDate(LocalDateTime.now());
        
        // 保存记录
        return gameUserAchievementMapper.insert(userAchievement) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<GameAchievementVO> checkAndUnlockPlayTimeAchievements(Long userId, Integer playTime) {
        // 如果未提供用户ID，则使用当前登录用户ID
        if (userId == null) {
            userId = SecurityUtils.getUserId();
        }
        
        // 获取可以解锁的成就列表
        List<GameAchievement> unlockableAchievements = baseMapper.getUnlockableAchievements(userId, "play_time", playTime);
        
        // 解锁成就并收集结果
        List<GameAchievementVO> newUnlockedAchievements = new ArrayList<>();
        if (unlockableAchievements != null && !unlockableAchievements.isEmpty()) {
            for (GameAchievement achievement : unlockableAchievements) {
                boolean unlocked = unlockAchievement(userId, achievement.getId());
                if (unlocked) {
                    // 转换为VO
                    GameAchievementVO vo = new GameAchievementVO();
                    vo.setId(achievement.getId());
                    vo.setName(achievement.getName());
                    vo.setDescription(achievement.getDescription());
                    vo.setIcon(achievement.getIcon());
                    vo.setGameId(achievement.getGameId());
                    vo.setUnlocked(true);
                    vo.setUnlockedDate(LocalDateTime.now());
                    
                    newUnlockedAchievements.add(vo);
                }
            }
        }
        
        return newUnlockedAchievements;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<GameAchievementVO> checkAndUnlockProgressAchievements(Long userId, Integer progress) {
        // 如果未提供用户ID，则使用当前登录用户ID
        if (userId == null) {
            userId = SecurityUtils.getUserId();
        }
        
        // 获取可以解锁的成就列表
        List<GameAchievement> unlockableAchievements = baseMapper.getUnlockableAchievements(userId, "progress", progress);
        
        // 解锁成就并收集结果
        List<GameAchievementVO> newUnlockedAchievements = new ArrayList<>();
        if (unlockableAchievements != null && !unlockableAchievements.isEmpty()) {
            for (GameAchievement achievement : unlockableAchievements) {
                boolean unlocked = unlockAchievement(userId, achievement.getId());
                if (unlocked) {
                    // 转换为VO
                    GameAchievementVO vo = new GameAchievementVO();
                    vo.setId(achievement.getId());
                    vo.setName(achievement.getName());
                    vo.setDescription(achievement.getDescription());
                    vo.setIcon(achievement.getIcon());
                    vo.setGameId(achievement.getGameId());
                    vo.setUnlocked(true);
                    vo.setUnlockedDate(LocalDateTime.now());
                    
                    newUnlockedAchievements.add(vo);
                }
            }
        }
        
        return newUnlockedAchievements;
    }
} 